package com.miyu.common.utils.filter.result;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

/**
 * 返回结果封装
 *
 * @author longmy
 * @date 2022/02/25
 */
public class ComResult {
    public static <T> ComResultPJ<T> success(T t){
        ComResultPJ<T> result = new ComResultPJ<>();
        result.setStatue(true);
        result.setCode(StateCode.SUCCESS.getCode());
        result.setMsg(StateCode.SUCCESS.getMsg());
        result.setData(t);
        return result;
    }

    /**
     * 错误异常处理 返回错误数据
     * code 错误码
     * @param code
     * @param <T>
     * @return
     */
    public static <T> ComResultPJ<T> error(String code){
        ComResultPJ<T> result = new ComResultPJ<>();
        result.setStatue(false);
        StateCode[] values = StateCode.values();
        for (StateCode StateCode : values) {
            if(StateCode.name().equals(code)){
                result.setMsg(StateCode.getMsg());
                result.setCode(StateCode.getCode());
                return result;
            }
        }
        result.setCode(500);
        result.setMsg("未知错误");
        return result;
    }

}

/**
 * 状态码
 *
 * @author longmy
 * @date 2022/02/25
 */
@Getter
@ToString
@AllArgsConstructor
enum StateCode{
    SUCCESS(200, "操作成功"),
    ERROR(500,"内部错误"),
    NOT_TOKEN(-1,"未登录"),
    INVALID_TOKEN(-2,"请重新登录"),
    TOKEN_TIMEOUT(-3,"登录超时"),
    BE_REPLACED(-4,"已在其他地方登录"),
    KICK_OUT(-5,"被踢下线");



    private Integer code;
    private String msg;
}
